# this script runs a series of other scripts to generates the measures of 
# defensive behavior examined in the paper and to produce 
# figures and certain tables for the paper

pacman::p_load(igraph, tidyverse, tidylog, haven, stringi, countrycode)

#Sys.setlocale('LC_ALL', 'russian')

# use files/directories corresponding to the date of owner search:
# 04012004 - before the Orange Revolution
# 11102006 - after the Orange Revolution
# 05112002 - "placebo" (2002)
search_dates <- c("04012004", "11102006", "05112002")

# generate defensive ownership measures for Delo/UP firms ----
# (main list of oligarch-controlled firms)
for (d in search_dates) {
  search_date <- d
  
  # build network from list of owners
  source("create_network_Delo_UP.R")

  # calculate measures of defensive ownership
  # oligarch in chain is counted only if it is an oligarch
  # from the same oligarch group as assigned to firm based on Delo/UP
  source("defensive_ownership_measures_Delo_UP.R")
  
  if (search_date == "04012004") {
    # alternative version: any oligarch in chain is counted
    # (Table A5 in the paper)
    source("defensive_ownership_measures_Delo_UP_alt_oligarch.R")
  }
}

# defensive ownership measures for Delo/UP firms based on JSCReg-only data ----
# and for all firms in JSCReg
for (d in search_dates) {
  search_date <- d
  
  # build network from list of owners (all firms in JSCReg)
  source("create_network_full_JSCReg.R")
  
  # calculate measures of defensive ownership (only Delo/UP firms)
  source("defensive_ownership_measures_Delo_UP_JSCReg_only.R")

  # calculate measures of defensive ownership (all firms in JSCReg)
  source("defensive_ownership_measures_full_JSCReg.R")
}

# plots of network graphs -------
search_date <- "04012004"
source("figure_1.R")